<?php


namespace App\Http\Model;

use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class SuggestRoomList extends Base
{
    protected $table = 'suggest_room_list';
    protected $roomTable = 'room';
    protected $roomTypeTable = 'room_type';
    protected $roomOpenLog = 'room_open_log';
    protected $roomOpenListKey = 'ROOM:OPENLIST';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    //前台获取推荐房间列表
    public function getSuggestRoom(){
        $data = DB::table($this->table)->where(['is_deleted'=>0,'is_show'=>1])->where('start_timestamp','<=',time())->where('end_timestamp','>=',time())->orderBy('sort','ASC')->get();
        $data = self::objToArray($data);
        $returnData = [];
        if(!empty($data)){
            $roomIds = array_column($data,'room_id');
            $roomInfos = DB::table($this->roomTable)->whereIn('room_id',$roomIds)->get(['name','room_id','image','room_type']);
            $roomInfos = self::objToArray($roomInfos);
            $roomInfos = array_column($roomInfos,null,'room_id');
            //查看直播中的房间
            $chacheKey = $this->roomOpenListKey;
            $list = Redis::HGETALL($chacheKey);
            $roomIds = [];
            foreach($list as $key=>$val) {
                [$roomId, $type] = explode(':', $key);
                $roomIds[] = $roomId;
            }
            $liveRoomList = array_values(array_unique($roomIds));
            //整理数据
            foreach($data as $k=>$v){
                $returnData[$k]['roomName'] = isset($roomInfos[$v['room_id']]['name']) ? $roomInfos[$v['room_id']]['name'] : '';//房间名称
                $returnData[$k]['roomImg'] = isset($roomInfos[$v['room_id']]['image']) ? build_image($roomInfos[$v['room_id']]['image']) : '';//房间图
                $returnData[$k]['roomId'] = $v['room_id'];//房间id
                $returnData[$k]['isLive'] = in_array($v['room_id'],$liveRoomList) ? 1 : 0;//1=直播中 0=未开播
                $row = DB::table($this->roomTypeTable)->where('type_id',$roomInfos[$v['room_id']]['room_type'])->first();
                $returnData[$k]['typeName'] = isset($row->type_name) ? $row->type_name : '';
                $returnData[$k]['imgHomeUrl'] = isset($row->img_home_url) ? build_image($row->img_home_url) : '';
            }
        }
        $finalData['count'] = count($returnData);//总条数
        $finalData['list'] = $returnData;//数据
        return $finalData;
    }

    //更改显示状态
    public function chageStatus($id, $showStatus, $isDelte){
        if(in_array($showStatus,[0,1])){
            $update['is_show'] = $showStatus;
        }

        if(in_array($isDelte,[0,1])){
            $update['is_deleted'] = $isDelte;
        }
        $update['updated_at'] = Carbon::now()->toDateTimeString();
        $status = DB::table($this->table)->where('id',$id)->update($update);
        return $status;
    }

    //添加或修改数据
    public function addOrUpdate($id, $room_id, $start_time, $end_time, $sort,$is_show){
        $data['room_id'] = $room_id;
        $data['sort'] = $sort;
        $data['is_show'] = 1;
        $data['start_timestamp'] = $start_time;
        $data['end_timestamp'] = $end_time;
        $data['is_show'] = $is_show;
    
        if(!empty($id)){
            $data['updated_at'] = Carbon::now()->toDateTimeString();
            $res = DB::table($this->table)->where('id',$id)->update($data);
        }else{
            //查询房间是否存在存在不能添加
            $check = DB::table($this->table)->where(['room_id'=>$room_id,'is_deleted'=>0])->exists();
            if($check){
                return false;
            }else{
                $data['created_at'] = Carbon::now()->toDateTimeString();
                $res = DB::table($this->table)->insert($data);
            }
            
        }
        return $res;
    }
}